=begin pod :tag<convert>

=TITLE 5to6-perlvar

=SUBTITLE Perl 5 to Perl 6 guide - special variables

=head1 DESCRIPTION

A (hopefully) comprehensive list of Perl 5 Special Variables with their Perl 6
equivalents with notes on variations between them where necessary.

=head1 NOTE

This document is an attempt to guide the reader from the
I<Special Variables> in Perl 5 to their equivalents in Perl 6.
For full documentation on the Perl 6 I<Special Variables>,
please see the Perl 6 documentation for each of them.


=head1 SPECIAL VARIABLES

X<|Special Variables (Perl 5)>

=head2 General Variables

=item $ARG

=item $_
X<|$_ (Perl 5)>

Thankfully, C<$_> is the general default variable as in Perl 5. The
main difference in Perl 6 is that you can now call methods on
it. For instance, Perl 5's C<say $_> can be rendered in Perl 6 as
C<$_.say>. Furthermore, as it is the default variable, you don't even
need to use the variable name. The previous example can also be
achieved by using C<.say>.

=item @ARG

=item @_
X<|@_ (Perl 5)>

As Perl 6 now has function signatures, your arguments can go there, rather
than depending on C<@_> for them. In fact, if you use a function signature, use
of C<@_> will spit at you telling it cannot override an existing signature.

If, however, you do not use a function signature, C<@_> will contain the
arguments you pass to the function as it did in Perl 5. Again, as with C<$_>,
you can call methods on it. Unlike C<$_> you cannot assume C<@_> as the
default variable for those methods to operate on (i. e. C<@_.shift> works,
C<.shift> does not).

=item $LIST_SEPARATOR

=item $"

Currently, there is no equivalent of the List Separator variable in Perl 6.
Design document L<S28|https://design.perl6.org/S28.html> says there isn't one,
so you probably don't want to hold your breath.

=item $PROCESS_ID

=item $PID

=item $$
X<|$$ (Perl 5)>

C<$$> is replaced in Perl 6 by C<$*PID>

=item $PROGRAM_NAME
=item $0
X<|$0 (Perl 5)>

You can access the program name in Perl 6 via C<$*PROGRAM-NAME>.

Note: C<$0> in Perl 6 is the variable holding the first captured value from a
regexp match (i. e. capture variables now start from C<$0> rather than C<$1>).

=item $REAL_GROUP_ID

=item $GID

=item $(

In Perl 6 the group information is handled by C<$*GROUP>,
which holds an object of type L<IntStr|/type/IntStr>
and therefore can be used either within a string
or a numeric context.
The group id is therefore obtained via C<$*GROUP.Numeric>, while the
group name via C<$*GROUP.Str>.

=item $EFFECTIVE_GROUP_ID

=item $EGID

=item $)

The effective group id does not appear to be currently provided by Perl 6.

=item $REAL_USER_ID

=item $UID

=item $<

In Perl 6 the user information is handled by C<$*USER>,
which holds an object of type L<IntStr|/type/IntStr>
and therefore can be used either within a string
or a numeric context (this is similar to how the group
information is handled by the C<$*GROUP> object).
The user id is therefore obtained via C<$*USER.Numeric>, while the
username via C<$*USER.Str>.


=item $EFFECTIVE_USER_ID

=item $EUID

=item $>

The effective user id does not appear to be currently provided by Perl 6.

=item $SUBSCRIPT_SEPARATOR

=item $SUBSEP

=item $;

The subscript separator variable is not included in Perl 6. Frankly, if your
Perl 5 code is using this, it's almost certainly really, really old.

=item $a

=item $b

C<$a> and C<$b> have no special meaning in Perl 6. C<sort()> does not
use them for anything special. They're just regular old variables.

This feature has been extended by having blocks with placeholder
parameters which are more versatile. Placeholder variables are created
with the C<^> twigil (e. g. C<$^z>. They can be used in a bare block or
in a subroutine without an explicit parameter list. The arguments to
the block are assigned to the placeholder variables in their Unicode
order. I. e. even if the variables appear in the block in the order
C<($^q, $^z, $^a)>, they will be I<assigned> in the order C<($^a, $^q,
$^z)>. Ergo:

=begin code
sort { $^a cmp $^z }, 1, 5, 6, 4, 2, 3;
# OUTPUT: «(1 2 3 4 5 6)␤»
sort { $^g cmp $^a }, 1, 5, 6, 4, 2, 3;
# OUTPUT: «(6 5 4 3 2 1)␤»
for 1..9 { say $^c, $^a, $^b; last }
# OUTPUT: «312␤»
=end code

For more on placeholder variables, see
L<this page|/language/variables#The_^_Twigil>

=item %ENV

%ENV has been replaced by %*ENV in Perl 6. Note that the keys of this hash may
not be exactly the same between Perl 5 and Perl 6. For example,
C<OLDPWD> is missing from Perl 6's %ENV.

=item $OLD_PERL_VERSION

=item $]

The running version of Perl 6 is kept by C<$*PERL> special variable, that is an object.
The running version is retrieved via C<$*PERL.version>, which returns something
like C<v6.c>; the full stringified version of the Perl interpreter is obtained
via C<$*PERL.Str>, which returns something like C<Perl 6 (6.c)>.


=item $SYSTEM_FD_MAX

=item $^F

Although the design documents (S28) indicate that this will likely
become C<$*SYS_FD_MAX>, this has not yet been implemented.

=item @F

[NEEDS FURTHER RESEARCH] A bit confusing at this point. Design doc S28
indicates that C<@F> in Perl 5 is replaced by C<@_> in Perl 6, but it's
unclear just how that works. On the other hand, it's currently something of a
moot point, as the Perl 5 to Perl 6 Translation doc indicates that the C<-a>
and C<-F> command-line switches are not yet implemented in rakudo.

=item @INC
X<|@INC (Perl 5)>

No longer exists in Perl 6.  Please use "use lib" to manipulate the
module repositories to be searched.  The closest thing to @INC is
really $*REPO.  But that works completely differently from @INC mostly
because of the precompilation capabilities of Perl 6.

     # Print out a list of compunit repositories
     .say for $*REPO.repo-chain;

=item %INC
X<|%INC (Perl 5)>

No longer exists in Perl 6.  Because each Repository is responsible for
remembering which modules have been loaded already. You can get a list
of all loaded modules (compilation units) like so:

=for code :skip-test<missing module>
use Test;
use MyModule;
say flat $*REPO.repo-chain.map(*.loaded); #-> (MyModule Test)

=item $INPLACE_EDIT

=item $^I

S28 suggests $*INPLACE_EDIT, but it does not yet exist.

=item $^M

S28 suggests $*EMERGENCY_MEMORY, but it does not yet exist.

=item $OSNAME

=item $^O

This is somewhat unclear. It probably depends on what you mean by "the name of
the operating system" as design document L<S28|https://design.perl6.org/S28.html>
has three different suggestions, all of which
give different answers.

There are currently three main objects containing information about the "running
environment":

=item C<$*KERNEL> provides information about the running Operating System kernel;
=item C<$*DISTRO> provides information about the Operating System distribution;
=item C<$*VM> provides information about the running backend machine for Perl 6.

All the above objects have methods in common:
=item C<version> provides the version number for that component;
=item C<name> provides the mnemonic name for that component;
=item C<auth> provides the known author(s) for that component.

As a short example, the following piece of code prints information about
all the above components:

=begin code
for $*KERNEL, $*DISTRO, $*VM -> $what {
    say $what.^name;
    say 'version '  ~ $what.version
        ~ ' named ' ~ $what.name
        ~ ' by '    ~ $what.auth;
}

# Kernel
# version 4.10.0.42.generic named linux by unknown
# Distro
# version 17.04.Zesty.Zapus named ubuntu by https://www.ubuntu.com/
# VM
# version 2017.11 named moar by The MoarVM Team
=end code

The C<Str> method on all of the above produces the I<short> version of the information,
at the current time the C<name>.

All the objects have other methods that can be useful when trying to identify the
exact running instance, for more information use <.^methods> to introspect all the above.

=item %SIG

[NEEDS FURTHER RESEARCH] No equivalent variable. S28 indicates that this
functionality is dealt with in Perl 6 by event filters and exception
translation.

=item $BASETIME

=item $^T

Replaced in Perl 6 by C<$*INIT-INSTANT>. Unlike in Perl 5, this is not in
seconds since epoch, but an L<Instant> object, which is measured in atomic
seconds, with fractions.

=item $PERL_VERSION

=item $^V

As with C<$]> this has been replaced with C<$*PERL.version>.

=item ${^WIN32_SLOPPY_STAT}

There is no analog to this in Perl 6.

=item $EXECUTABLE_NAME

=item $^X

This has been replaced by C<$*EXECUTABLE-NAME>.
Note that there is also C<$*EXECUTABLE>, which is an C<IO> object in Perl 6.

=head2 Variables related to regular expressions

=head3 Performance issues

As shown below, C<$`>, C<$&>, and C<$'> are gone from Perl 6, primarily
replaced by variations on C<$/> and, with their elimination, the
associated performance issues in Perl 5 do not apply.

=item $<I<digits>> ($1, $2, ...)

These existing variables do the same thing in Perl 6 as they do in Perl 5,
except that they now start at C<$0> rather than C<$1>. Furthermore, they are
synonyms for indexed items in the match variable C<$/>. I. e. C<$0> is equivalent
to C<$/[0]>, C<$1> is equivalent to C<$/[1]>, etc.

=item $MATCH

=item $&

C<$/> now contains the L<match|/type/Match> object, so the Perl 5 behavior of C<$&> can
be obtained by stringifying it, i. e. C<~$/>.

Please note that while C<$/.Str> should also work,
C<~$/> is currently the more common idiom.

=item ${^MATCH}

Since the former performance issues are done away with, this variable is
not of use in Perl 6.

=item $PREMATCH

=item $`

Replaced by C<$/.prematch>.

=item ${^PREMATCH}

Since the former performance issues are done away with, this variable is
not of use in Perl 6.

=item $POSTMATCH

=item $'

Replaced by C<$/.postmatch>.

=item ${^POSTMATCH}

Since the former performance issues are done away with, this variable is
not of use in Perl 6.

=item $LAST_PAREN_MATCH

=item $+

Does not exist in Perl 6, but you can get the same information using C<$/[*-
1].Str> (C<$/[*-1]> would be the match object, not the actual string).

If you want to I<understand> why that works, you can look at these documents:

=item L<[ ] routine|/routine/%5b%20%5d#language_documentation_operators>

=item L<Whatever|/type/Whatever>

...and possibly

=item L<https://design.perl6.org/S02.html#line_1126>

...though the design documents are not always up to date.

=item $LAST_SUBMATCH_RESULT

=item $^N

S28 suggests C<$*MOST_RECENT_CAPTURED_MATCH>, but there does not seem to be
any implemented variable that matches C<$^N>.

=item @LAST_MATCH_END

=item @+

As with most regular expression related variables, this functionality
is, at least in part, moved to the C<$/> variable in Perl 6. Or, in this
case, the numbered variables that alias to the indexes of it. The offset
is found by using the C<.to> method. I. e. the first offset is
C<$/[0].to>, which is synonymous with C<$0.to>. The value Perl 5
provides as C<$+[0]> is provided by C<$/.to>.

=item %LAST_PAREN_MATCH

=item %+

Once again, we move over to C<$/>. The former C<$+{$match}> is
C<$/{$match}>.

=item @LAST_MATCH_START

=item @-

Similarly to C<@+> being replaced by using the C<.to> method, C<@-> is
replaced by using the C<.from> method on C<$/> and its variations. The
first offset is C<$/[0].from> or the equivalent C<$0.from>. Perl 5's C<$-
[0]> is C<$/.from>.


=item %LAST_MATCH_START

=item %-

Much like C<%+>, a use of C<%-{$match}> would be replaced with C<$/{$match}>.

=item $LAST_REGEXP_CODE_RESULT

=item $^R

No equivalent.

=item ${^RE_DEBUG_FLAGS}

No equivalent.

=item ${^RE_TRIE_MAXBUF}

No equivalent.

=head2 Variables related to filehandles

=item $ARGV

The name of the current file when reading lines can be obtained through
C<$*ARGFILES.filename>.

=item @ARGV

C<@*ARGS> contains the command line arguments.

=item ARGV

This has been replaced by C<$*ARGFILES>.

=item ARGVOUT

As the C<-i> command line switch has not yet been implemented, there is not
yet an equivalent of C<ARGVOUT>.

=item $OUTPUT_FIELD_SEPARATOR

=item $OFS

=item $,

Currently no obvious equivalent.

=item $INPUT_LINE_NUMBER

=item $NR

=item $.

No direct replacement exists.

When iterating using L<lines> method from L<IO::Path> or L<IO::Handle> types,
you can L<zip|/language/operators#index-entry-Z_%28zip_meta_operator%29> with
a L<Range>:

=begin code
for 1..* Z "foo".IO.lines -> ($ln, $text) {
    say "$ln: $text"
}
# OUTPUT:
# 1: a
# 2: b
# 3: c
# 4: d
=end code

For L<IO::CatHandle> types
(of which L«C<$*ARGFILES>|/language/variables#index-entry-%24%2AARGFILES» is
one), you can use L«C<on-switch>|/type/IO::CatHandle#method_on-switch» hook
to reset line number on handle switch, and increment it manually.
See also L«C<IO::CatHandle::AutoLines>|https://modules.perl6.org/repo/IO::CatHandle::AutoLines»
and L«C<LN>|https://modules.perl6.org/repo/LN» modules that simplify this
operation.

=item $INPUT_RECORD_SEPARATOR

=item $RS

=item $/

This is accessed through the C<.nl-in> method on the filehandle. E. g.
C<$*IN.nl-in>.

=item $OUTPUT_RECORD_SEPARATOR

=item $ORS

=item $\

This is accessed through the C<.nl-out> method on the filehandle. E. g.
C<$*OUT.nl-out>.

=item $OUTPUT_AUTOFLUSH

=item $|

No global alternative available. TTY handles are unbuffered by default, for
others, set L<out-buffer> to zero or use C<:!out-buffer> with L<open> on a
specific L<IO::Handle>.

=item ${^LAST_FH}

Not implemented in Perl 6.

=head3 Variables related to formats

There are no built-in formats in Perl 6.

=head2 Error Variables

Because of how error variables have changed in Perl 6, they will not be detailed
here individually.

To quote the Perl 6 L<docs|syntax/$!>, "$! is the error variable." That's it.
All the error variables appear to have been eaten by $!. As with the rest of
Perl 6, it's  an object that will return various things depending on the type of error
or L<exception|/type/Exception>.

In particular, when dealing with L<exceptions|/type/Exception> the C<$!> provides information
about the thrown exception, assuming the program has not halted:

=begin code
try {
    fail "Boooh";
    CATCH {
        # within the catch block
        # the exception is placed into $_
        say 'within the catch:';
        say $_.^name ~ ' : ' ~ $_.message;
        $_.resume; # do not abort
    }
}

# outside the catch block the exception is placed
# into $!
say 'outside the catch:';
say $!.^name ~ ' : ' ~ $!.message;
=end code

and the above code produces the following output

=for code :lang<shell>
within the catch:
X::AdHoc : Boooh
outside the catch:
X::AdHoc : Boooh

therefore, as stated before, the C<$!> variable holds the exception object.

=head2 Variables related to the interpreter state

=item $COMPILING

=item $^C

=item $^D

Currently no equivalents for either of these variables.

=item ${^ENCODING}

Although deprecated in Perl 5, this may have some sort of equivalent in
C<$?ENC>, but this is far from clear.

=item ${^GLOBAL_PHASE}

No Perl 6 equivalent.

=item $^H

=item %^H

=item ${^OPEN}

There may or may not be equivalents of these in Perl 6, but they're internal
and you shouldn't be messing with them in the first place - certainly not if
your understanding of Perl 6 requires you to read this document...

=item $PERLDB

=item $^P

The chance of the Perl 6 debugger resembling the Perl 5 debugger is slim
at best, and at this point there does not seem to be an equivalent of
this variable.

=item ${^TAINT}

S28 claims this variable is "pending". Not currently in Perl 6.

=item ${^UNICODE}

=item ${^UTF8CACHE}

=item ${^UTF8LOCALE}

These Unicode-related variables do not appear to exist in Perl 6, but -
maybe? - could have analogs in C<$?ENC> somewhere. This, however, is
totally unconfirmed.

=head2 Deprecated and removed variables

It should go without saying that, as these have been removed from Perl 5
already, there should be no need to tell you how to use them in Perl 6.

=end pod

# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6
